<?php

namespace SinoPHP\BbdBoss\Api;

/**
 * 虚拟节点同步接口
 *
 * Params:
 *      data:startTime    (int)     查询起始时间
 *      data:endTime      (int)     查询结束时间
 */
class VirtualNode extends ApiAbstract {

    public function v_1_0_0() {
        $params = $this->requestParams;

        // $this->checkToken(); // 校验TOKEN
        $where = [];
        if (isset($params['startTime']) && isset($params['endTime'])) {
            $where = ['update_time|<>' => [$params['startTime'], $params['endTime']]];
        }
        $fields = 'virtual_node_id,physical_node_id,server_ip_id,real_ip_ids,is_delete,update_time';
        $virtualNode = model('VirtualNode')->getList($fields, $where);
        if (empty($virtualNode)) {
            $this->success($virtualNode);
        }
        $ipWhere['belong_type'] = 0;
        $ipWhere['is_delete'] = 0;
        $ipWhere['status'] = 1;
        $ipData = model('ServerIp')->getList('ip_id,ip', $ipWhere);

        $ipList = [];
        foreach ($ipData as $value) {
            $ipList[$value['ip_id']] = $value['ip'];
        }

        foreach ($virtualNode as &$value) {
            $ipIds = explode(',', $value['real_ip_ids']);
            foreach ($ipIds as $ipId) {
                $value['real_ip_str'][] = $ipList[$ipId];
            }
            $value['server_ip_str'] = $ipList[$value['server_ip_id']];
            unset($value['server_ip_id']);
            unset($value['real_ip_ids']);
            $value['virtual_node_id'] = (int)$value['virtual_node_id'];
            $value['is_delete'] = (int)$value['is_delete'];
            $value['update_time'] = (int)$value['update_time'];
        }

        $this->success($virtualNode);
    }

}